const pool = require('../modules/tools/getPool');

const insertAdmin = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('INSERT INTO admin (account,password) VALUES ("admin","123123123");', (error, rows, fields)=>{
			if (error) {
				reject(error,1)
			}
			resolve(rows)
		})
    })
}

const insertSales = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('INSERT INTO clerk (account, password, name, department, type) VALUES ("zhangsan","123123123","张三","sales","sales");', (error, rows, fields)=>{
			if (error) {
				reject(error,2)
			}
			resolve(rows)
		})
    })
}

const insertBuyer = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('INSERT INTO clerk (account, password, name, department, type) VALUES ("lisi","123123123","李四","buyer","buyer");', (error, rows, fields)=>{
			if (error) {
				reject(error,2)
			}
			resolve(rows)
		})
    })
}

const insertCheck = function(connection){
    return new Promise((resolve, reject)=>{
		connection.query('INSERT INTO clerk (account, password, name, department, type) VALUES ("wangwu","123123123","王五","check","check");', (error, rows, fields)=>{
			if (error) {
				reject(error,2)
			}
			resolve(rows)
		})
    })
}

pool.getConnection((err, connection)=>{
	if (err) {
		console.error(err);
		return;
	}

	const start = async function(){

		await insertAdmin(connection);
		await insertSales(connection);
		await insertBuyer(connection);
		await insertCheck(connection);

	}

	start().then(v=>{

		console.log("success");

	})
	.catch((e,n)=>{
		console.log(e);
		console.log(n);
	})

});